// pages/sourcedet/index.js
import baseRequest from '../../utils/base-request';
import config from '../../config/index';
import RewardedAds from '../../utils/rewarded-ads';
const instanceRewardedAds = new RewardedAds(config.copySoftLinkUpAdCode);

// 复制内容到剪贴板
function copyContent(content) {
  wx.setClipboardData({
    data: content,
    success: function () {
      wx.showToast({
        title: '复制成功!',
        duration: 1000,
        icon: 'success'
      });
    }
  })
}
const tips = '获得此链接需要观看完免费广告哦';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    detailData: {},
    adBannerCode: config.softDetailAdCdoe,
    itemid: 0,
    copyTip: ''
  },

  onAdClosed(e){
    console.log(e);
    const {isEnded} = e || {};
    if(!isEnded){
      wx.showToast({
        title: '观看未结束哦~',
        icon:'error',
        duration:1000
      })
    }else{
      wx.showToast({
        title: '成功获得奖励，请直接点击复制~',
        icon:'success',
        duration:1000
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    instanceRewardedAds.resetAd();
    const itemid = options['itemid'];
    this.setData({
      itemid
    })
    this.loadDetail(itemid);
    //访问量+1
    this.setViewCountAdd(itemid);
    //初始化激励性广告
    instanceRewardedAds.adInit(this.onAdClosed);
  },
  parseContent(content) {
    if (!content) return '暂无介绍';
    const arr = content.split('======');
    const list = arr.map((item, index) => {
      let type = 'text';
      if (index === 0)
        type = 'softname';
      else if (item.indexOf('http') > -1) {
        type = 'img';
      }
      return {
        value: item,
        type
      }
    })
    console.log(list);
    return list;
  },
  setViewCountAdd(itemid) {
    baseRequest('/_api/source-api/set-vcount', {
      id: itemid
    });
  },
  loadDetail(itemid) {
    wx.showLoading({
      title: '详情加载中...',
    })
    baseRequest('/_api/source-api/get-detail', {
      id: itemid
    }).then(res => {
      let detail = res.length ? res[0] : {};
      detail = {
        ...detail,
        conList: this.parseContent(detail.sourceContent)
      }
      delete detail.sourceContent;
      this.setData({
        detailData: detail,
        copyTip: detail.width ? tips : ''
      })
      wx.hideLoading();
    })
  },
  onUnload(){
    instanceRewardedAds.resetAd();
  },
  handleCopy() {
    const link = this.data.detailData.redirectUrl;
    const {
      width
    } = this.data.detailData || {};
    if (width === 1) {
      wx.showToast({
        title: '准备中...',
        icon:'loading',
        duration:1000
      })
      //展示激励性广告
      if(!instanceRewardedAds.isEnded){
        instanceRewardedAds.showAd();
        return;
      }
      copyContent(link);
    } else {
      copyContent(link)
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    const {
      nameStr,
      imgSrc
    } = this.data.detailData || {};
    return {
      title: nameStr,
      path: '/pages/sourcedet/index?itemid=' + this.data.itemid,
      imageUrl: imgSrc
    }
  }
})